$(function(){
	this.$body = $('.gray-bg');
	//[data-setstatus]
	this.$body.on('click','[data-setstatus]',function(){
	    action = this.getAttribute("data-setstatus");
	    param = JSON.stringify( this.getAttribute("data-param").split(",") );
	    $.post(action,{param:param},function(res){ response(res); });
	});

	//[data-upload]
	this.$body.on('click','[data-upload]',function(){
		multiple = this.getAttribute("data-multiple");
		targetpath = this.getAttribute("data-upload");
		action = this.getAttribute("data-action");
		data_ext = this.getAttribute("data-ext");
	    var url = '/admin/attachment/uploadfile?multiple='+multiple+'&targetpath='+targetpath+'&action='+action+'&data_ext='+data_ext;
		layer.open({
			type:2,
			title:"上传附件",
			area:['80%','50%'],
			content:url,
			btn:['确定'],
			yes:function(){
				window.location.reload();
			},
			cancel:function(){
				window.location.reload();
			}
		});
	});

	//[data-delete]
	this.$body.on('click','[data-delete]',function(){
	    action = this.getAttribute("data-delete");
	    param = JSON.stringify( this.getAttribute("data-param").split(",") );
	    if(param.length <= 4){
	    	layer.msg('请选择数据');
	    }else{
	    	layer.open({
		    	type:1,
		    	title:"提示",
		    	content:'<div class="alert alert-danger" role="alert">确认删除这些数据吗?</div>',
		    	btn:['确认','取消'],
		    	yes:function(){
		    		$.post(action,{param:param},function(res){
		    			layer.closeAll();
		    			response(res);
		    		});
		    	},
		    	btn2:function(){
		    		layer.closeAll();
		    	}
		    })
	    }
	});

	//[data-add]
	this.$body.on('click','[data-add]',function(){
		action = this.getAttribute("data-add");
		param = JSON.stringify( this.getAttribute("data-param").split(",") );

		if(param.length <= 4){
			layer.msg('请选择数据');
		}else{
			layer.open({
				type:1,
				title:"提示",
				content:'<div class="alert alert-danger" role="alert">确认添加这些数据吗?</div>',
				btn:['确认','取消'],
				yes:function(){
					$.post(action,{param:param},function(res){
						// layer.closeAll();
						response(res);
					});
				},
				btn2:function(){
					layer.closeAll();
				}
			})
		}
	});
})

function response(res){
	if(res.code == 1){
		callbackSuccess(res);
	}else{
		callbackError(res);
	}
}

function callbackSuccess(res){
	$('#stop-opration').css({'display':'block'});
    layer.msg(res.msg);
    setTimeout(function(){
        window.location.reload();
    },600);
}
function callbackError(res){
    layer.msg(res.msg);
}


//闹钟
$(function(){
	if($.cookie('AlarmClock') == '1'){
		var AlarmClockTmp = true;
	}else{
		var AlarmClockTmp = false;
	}

	layui.use('laydate', function(){
  		var laydate = layui.laydate;
		laydate.render({
			elem: '#AlarmClockPicker',type: 'time'
		});
	});
	$('.alarm-button').on('click',function(){
		layer.open({
			type:1,
			title:'设置闹钟',
			skin:'layui-layer-lan',
			content:$("#AlarmClockForm"),
			btn:['确定','取消'],
			yes:function(){

				var _h = $("#AlarmClockForm_h").val();
				var _i = $("#AlarmClockForm_i").val();
				var _s = $("#AlarmClockForm_s").val();
				if(isNaN(_h) || isNaN(_i) || isNaN(_s)){
					layer.msg('时间填写错误，请重新填写',{anim:6});
					return;
				}
				if(_h <= 0 && _i <= 0 && _s <= 5){
					layer.msg('填写时间太短，请重新填写',{anim:6});
					return;
				}
				var _expires = _h / 24 + _i / 60 / 24 + _s / 3600 /24;
				$.cookie('AlarmClock',"1",{expires:_expires});
				var AlarmClockTips = $("#AlarmClockTips").val();
				if(AlarmClockTips == ''){
					var mydate = new Date();
					AlarmClockTips = '您于' + mydate.getHours() + ":" + mydate.getMinutes() + ":" + mydate.getSeconds() + "设置的闹钟";
				}
				$.cookie('AlarmClockTips',AlarmClockTips,{expires:2});
				layer.closeAll();
				layer.msg(Number(_h) + '小时' + Number(_i) + '分钟' + Number(_s) + '秒 后闹钟提醒',{time:1000},function(){
					window.location.reload();
				});
			}
		})
	});
	setInterval(function(){
		if($.cookie('AlarmClock') != '1' && AlarmClockTmp == true){
			alert("您设置的闹钟时间到了:" + $.cookie('AlarmClockTips'))
			AlarmClockTmp = false;
			$.cookie('AlarmClockTips',{expires:-1});
		}
	},1000);
})
